<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${mapperPackage}.${tableClass.shortClassName}${mapperSuffix}">
    <#if tableClass.allFields?has_content && tableClass.pkFields?has_content >
            <!-- 字段映射模块 包括  一对一 一对多 多对多 查询 都在里面进行标签映射 -->
            <resultMap id="BaseResultMap" type="${tableClass.fullClassName}" >
                <#list tableClass.pkFields as field>
                    <id column="${field.columnName}" property="${field.fieldName}" jdbcType="${field.jdbcType}" />
                </#list>
            <#list tableClass.allFields as field>
                <#list tableClass.pkFields as pkfield>
                    <#if tableClass.pkFields?has_content && pkfield.columnName!=field.columnName>
                    <result column="${field.columnName}" property="${field.fieldName}" jdbcType="${field.jdbcType}"/>
                    </#if>
                </#list>
            </#list>
            </resultMap>


            <!-- sql 字段模块-->
           <sql id="selectmodule">
           <#list tableClass.allFields as field>
               <#if field_index gte tableClass.allFields?size-1>
                    ${field.columnName}
                   <#else/>
                    ${field.columnName},
               </#if>
           </#list>
           </sql>

        <!-- 插入 -->
       <insert id="insert" parameterType="${tableClass.fullClassName}">
           insert into ${tableClass.tableName}(
           <#list tableClass.allFields as field>
               <#list tableClass.pkFields as pk>
                   <#if field.columnName!=pk.columnName>
                   <#if field_index gte tableClass.allFields?size-1>
                       ${field.columnName}
                       <#else/>
                       ${field.columnName},
                   </#if>
                   </#if>
               </#list>
           </#list>
           )
           values(
           <#list tableClass.allFields as field>
               <#list tableClass.pkFields as pk>
                   <#if field.fieldName!=pk.fieldName>
                       <#if field_index gte tableClass.allFields?size-1>
                           ${r'#'}{ ${field.fieldName},jdbcType=${field.jdbcType} }
                           <#else/>
                           ${r'#'}{ ${field.fieldName},jdbcType=${field.jdbcType} },
                       </#if>
                   </#if>
               </#list>
           </#list>
           )
       </insert>


        <!-- 可选性插入-->
        <insert id="insertSelective" parameterType="${tableClass.fullClassName}" >
            insert into ${tableClass.tableName}
            <trim prefix="(" suffix=")" suffixOverrides="," >
                <#list tableClass.allFields as field>
                    <if test="${field.fieldName} != null" >
                        ${field.columnName},
                    </if>
                </#list>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides="," >
                <#list tableClass.allFields as field>
                <if test="${field.fieldName} != null" >
                    ${r'#'}{ ${field.fieldName},jdbcType=${field.jdbcType}},
                </if>
                </#list>
            </trim>
        </insert>


        <!--根据ID主键查询-->
        <@select statement="selectOne"  parameterType="${tableClass.fullClassName}"/>


        <!--根据ID主键查询-->
        <@select statement="selectById"  parameterType="${tableClass.pkFields[0].fullTypeName}"/>


        <!--根据主键ID删除 -->
        <@delete "delete" "${tableClass.fullClassName}"/>

        <!--根据主键ID删除 -->
        <@delete "deleteById" "${tableClass.pkFields[0].fullTypeName}"/>


        <!-- 根据主键ID批量删除-->
        <delete id="deleteBatch" parameterType="${tableClass.pkFields[0].fullTypeName}">
            delete FROM ${tableClass.tableName}  WHERE   ${tableClass.pkFields[0].columnName}  IN
            <foreach item="${tableClass.pkFields[0].fieldName}" collection="array" open="(" separator="," close=")">
                ${r'#'}{${tableClass.pkFields[0].fieldName},jdbcType=${tableClass.pkFields[0].jdbcType}}
            </foreach>
        </delete>


        <!-- 根据主键ID更新-->
        <update id="update" parameterType="${tableClass.fullClassName}" >
            update ${tableClass.tableName}
            set
             <#list tableClass.allFields as field>
                 <#list tableClass.pkFields as pk>
                     <#if field.columnName!=pk.columnName>
                         <#if field_index gte tableClass.allFields?size-1>
                             ${field.columnName} = ${r'#'}{${field.fieldName},jdbcType=${field.jdbcType}}
                             <#else/>
                             ${field.columnName} = ${r'#'}{${field.fieldName},jdbcType=${field.jdbcType}},
                         </#if>
                     </#if>
                 </#list>
             </#list>
            where
           ${tableClass.pkFields[0].columnName} = ${r'#'}{${tableClass.pkFields[0].fieldName},jdbcType=${tableClass.pkFields[0].jdbcType}}
        </update>


        <!-- 根据主键ID可选性更新-->
        <update id="updateByPrimaryKeySelectiveAdvertslot" parameterType="${tableClass.fullClassName}" >
            update ${tableClass.tableName}
            <set>
                <#list tableClass.allFields as field>
                    <if test="${field.fieldName} != null" >
                        ${field.columnName} = ${r'#'}{${field.fieldName},jdbcType=${field.jdbcType}},
                    </if>
                </#list>
            </set>
            where   ${tableClass.pkFields[0].columnName} = ${r'#'}{${tableClass.pkFields[0].fieldName},jdbcType=${tableClass.pkFields[0].jdbcType}}
        </update>
    </#if>

    <#macro  delete  statement parameterType   >
        <delete id="${statement!}" parameterType="${parameterType!}">
            delete from	${tableClass.tableName}  WHERE ${tableClass.pkFields[0].columnName}=${r'#'}{${tableClass.pkFields[0].fieldName},jdbcType=${tableClass.pkFields[0].jdbcType}}
        </delete>
    </#macro>

    <#macro select statement parameterType >
        <select id="${statement!}" parameterType="${parameterType}" resultMap="BaseResultMap">
            select
            <include refid="selectmodule" />
            from ${tableClass.tableName}
            where
            ${tableClass.pkFields[0].columnName} = ${r'#'}{${tableClass.pkFields[0].fieldName},jdbcType=${tableClass.pkFields[0].jdbcType}}
        </select>
    </#macro>

</mapper>